package com.ksd.modules.members.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.members.entity.AdminUser;
import com.ksd.modules.members.entity.AdminUserVipBuy;
import com.ksd.modules.members.mapper.AdminUserVipBuyMapper;
import com.ksd.modules.members.po.AdminUserVipBuySavePO;
import com.ksd.modules.members.po.AdminUserVipBuySearchPO;
import com.ksd.modules.members.service.IAdminUserService;
import com.ksd.modules.members.service.IAdminUserVipBuyService;
import com.ksd.modules.members.service.IAdminUserVipService;
import com.ksd.modules.members.vo.AdminUserVipBuyVo;
import com.ksd.modules.members.vo.AdminUserVipVo;
import com.ksd.modules.members.vo.AdminUserVo;
import com.ksd.utils.date.TmDateUtil;
import com.ksd.utils.snow.SnowflakeIdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminUserVipBuyServiceImpl extends BaseServiceImpl<AdminUserVipBuyMapper, AdminUserVipBuy> implements IAdminUserVipBuyService {

    @Autowired
    private IAdminUserService adminUserService;
    @Autowired
    private IAdminUserVipService adminUserVipService;

    /**
     * 查询列表
     *
     * @param adminUserVipBuySearchPO
     * @return
     */
    public Page<AdminUserVipBuyVo> findAdminUserVipBuys(AdminUserVipBuySearchPO adminUserVipBuySearchPO) {
        Page<AdminUserVipBuy> page = new Page<>(adminUserVipBuySearchPO.getPageNo(), adminUserVipBuySearchPO.getPageSize());
        LambdaQueryWrapper<AdminUserVipBuy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminUserVipBuy::getSystemId, SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserVipBuySearchPO.getPayMethod()), AdminUserVipBuy::getPayMethod, adminUserVipBuySearchPO.getPayMethod());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminUserVipBuySearchPO.getCode()), AdminUserVipBuy::getCode, adminUserVipBuySearchPO.getCode());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminUserVipBuySearchPO.getOrderno()), AdminUserVipBuy::getOrderno, adminUserVipBuySearchPO.getOrderno());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminUserVipBuySearchPO.getUuid()), AdminUserVipBuy::getUuid, adminUserVipBuySearchPO.getUuid());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminUserVipBuySearchPO.getPhone()), AdminUserVipBuy::getPhone, adminUserVipBuySearchPO.getPhone());
        // 模糊匹配
        if (StringUtils.isNotEmpty(adminUserVipBuySearchPO.getKeyword())) {
            lambdaQueryWrapper.and(c -> c.like(
                            AdminUserVipBuy::getTitle, adminUserVipBuySearchPO.getKeyword())
                    .or().like(AdminUserVipBuy::getNickname, adminUserVipBuySearchPO.getKeyword())
                    .or().like(AdminUserVipBuy::getPhone, adminUserVipBuySearchPO.getKeyword())
                    .or().like(AdminUserVipBuy::getUsername, adminUserVipBuySearchPO.getKeyword())
            );
        }

        lambdaQueryWrapper.orderByDesc(AdminUserVipBuy::getCreateTime);
        Page<AdminUserVipBuy> adminUserVipBuyPage = this.page(page, lambdaQueryWrapper);
        Page<AdminUserVipBuyVo> adminUserVipBuyVoPage = tranferPageBo(adminUserVipBuyPage, AdminUserVipBuyVo.class);
        return adminUserVipBuyVoPage;
    }


    /**
     * 保存和更新
     *
     * @param adminUserVipBuySavePO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AdminUserVipBuyVo saveUpdateAdminUserVipBuy(AdminUserVipBuySavePO adminUserVipBuySavePO) {
        AdminUserVipBuy adminUserVipBuy = tranferBo(adminUserVipBuySavePO, AdminUserVipBuy.class);
        adminUserVipBuy.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminUserVipBuy.setSystemName(SystemIdThrealLocal.get().getSystemName());
        boolean b = this.saveOrUpdate(adminUserVipBuy);
        AdminUserVipBuyVo adminUserVipBuyVo = tranferBo(adminUserVipBuy, AdminUserVipBuyVo.class);
        return adminUserVipBuyVo;
    }

    /**
     * 根据ID获取明细
     *
     * @param id
     * @return
     */
    public AdminUserVipBuyVo getAdminUserVipBuyById(Long id) {
        AdminUserVipBuy adminUserVipBuy = this.getById(id);
        AdminUserVipBuyVo adminUserVipBuyVo = tranferBo(adminUserVipBuy, AdminUserVipBuyVo.class);
        return adminUserVipBuyVo;
    }


    /**
     * 单个物理删除
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeAdminUserVipBuyById(Long id) {
        boolean b = this.removeById(id);
        return b;
    }


    /**
     * 批量物理删除
     *
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchRemovesAdminUserVipBuy(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }

    /**
     * 用户绑定小册订单
     *
     * @param uuid
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean bindUserOrder(String uuid, Long opid) {
        AdminUserVipBuy adminUserVipBuy = new AdminUserVipBuy();
        AdminUserVo adminUserVo = adminUserService.getAdminUserByUUId(uuid);
        AdminUserVipVo adminUserVipVo = adminUserVipService.getAdminUserVipById(opid);
        adminUserVipBuy.setVipId(opid);
        adminUserVipBuy.setOpid(adminUserVo.getId());
        adminUserVipBuy.setNickname(adminUserVo.getNickName());
        adminUserVipBuy.setUsername(adminUserVo.getUserName());
        adminUserVipBuy.setAvatar(adminUserVo.getAvatar());
        adminUserVipBuy.setUuid(adminUserVo.getUuid());
        adminUserVipBuy.setPhone(adminUserVo.getTelephone());
        adminUserVipBuy.setAddress(adminUserVo.getAddress());

        adminUserVipBuy.setTitle(adminUserVipVo.getTitle());
        adminUserVipBuy.setDescription(adminUserVipVo.getNote());
        adminUserVipBuy.setCode("");
        adminUserVipBuy.setPrice(adminUserVipVo.getPrice());

        adminUserVipBuy.setOrderno(String.valueOf(new SnowflakeIdWorker(7, 8).nextId()));
        adminUserVipBuy.setTradeno(String.valueOf(new SnowflakeIdWorker(8, 8).nextId()));
        adminUserVipBuy.setPayMethod(4);
        adminUserVipBuy.setPayMethodName("人工");

        adminUserVipBuy.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminUserVipBuy.setSystemName(SystemIdThrealLocal.get().getSystemName());
        adminUserVipBuy.setOrderJson("");
        boolean b = this.saveOrUpdate(adminUserVipBuy);
        if (b) {
            AdminUser adminUser = new AdminUser();
            adminUser.setId(adminUserVo.getId());
            // 设置vip类型
            adminUser.setVipFlag(adminUserVipVo.getVipType());
            if (Objects.isNull(adminUserVo.getVipTime())) {// 第一次开会员
                adminUser.setVipTime(TmDateUtil.addDay(new Date(), adminUserVipVo.getVipDays()));
            } else {
                if (adminUserVo.getVipTime().before(new Date())) { // 过期的会员
                    adminUser.setVipTime(TmDateUtil.addDay(new Date(), adminUserVipVo.getVipDays()));
                } else {
                    // 续期会员
                    adminUser.setVipTime(TmDateUtil.addDay(adminUserVo.getVipTime(), adminUserVipVo.getVipDays()));
                }
            }
            // 同步和修改
            adminUserService.updateById(adminUser);
        }
        return b;
    }
}
